﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Omu.AwesomeMvc;
using TCM;
using TCM.Models;

namespace TCM.Controllers{
    public class StatusIDLookupController : Controller
    {
        private TCMContext Db = new TCMContext();
        public ActionResult GetItem(int? v)
        {
            var o = Db.RecordStates.SingleOrDefault(f => f.StatusID == v) ?? new RecordState();

            return Json(new KeyContent(o.StatusID, o.StatusName));
        }
        public ActionResult SearchForm()
        {
            return View();
        }

        public ActionResult Search(string search, int page)
        {
            search = (search ?? "").ToLower().Trim();
            var list = Db.RecordStates.Where(f => f.StatusName.ToLower().Contains(search)).Select(o => new { StatusID = o.StatusID, StatusName = o.StatusName });
            return Json(new AjaxListResult
            {
                Items = list.Page(page ,7).Select(o => new KeyContent(o.StatusID, o.StatusName)),
                More = list.Count() > page * 7
            });
        }
    }

}